#pragma once

#include<iostream>
#include<strings.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string>

class Inetaddr{
    public:
    Inetaddr(){}
    Inetaddr(const struct sockaddr_in& addr)
    {
        Setaddr(addr);
    }

    void Setaddr(const struct sockaddr_in& addr)
    {
        _addr = addr;
        _ip = inet_ntoa(_addr.sin_addr);
        _port = _addr.sin_port;
    }
    Inetaddr(const std::string ip ,uint16_t port)
    :_ip(ip),
    _port(port)
    {
        bzero(&_addr,0);
        _addr.sin_family = AF_INET;
        _addr.sin_port = htons(_port);
        inet_pton(AF_INET,_ip.c_str(),&_addr.sin_addr);
    }

    Inetaddr(uint16_t port):_port(htons(port)),_ip("0")
    {
        _addr.sin_family = AF_INET;
        _addr.sin_port = _port;
        _addr.sin_addr.s_addr = INADDR_ANY;
    }

    std::string String_addr() const
    {
        return _ip + ": "+std::to_string(_port);
    }

    const struct sockaddr_in& Getaddr() const
    {
        return _addr;
    }

    struct sockaddr* Getsockaddr()
    {
        return (struct sockaddr*)&_addr;
    }


    const socklen_t Lenaddr() const
    {
        return sizeof(_addr);
    }



    int InetIp() const
    {
        return _addr.sin_addr.s_addr;
    }

    bool operator<(const Inetaddr& addr) const{
        // 假设 Inetaddr 有一个 uint32_t ip 成员
        //struct in_addr tmp;
        //int i = inet_addr(_ip.c_str());
        ///int i = inet_pton()
        return InetIp() < addr.InetIp();
    }


    ~Inetaddr(){}

    private:
    struct sockaddr_in _addr;
    std::string _ip;
    uint16_t _port;

};